﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace OMSService
{
    public class CreditCardManager
    {
        private static CreditCard GetCreditCardById(int id)
        {
            //TODO: Error handling
            CreditCard creditCard;
            using (OMSModelContainer context = new OMSModelContainer())
            {
                creditCard = (from m in context.CreditCards where m.Id == id select m).First();
            }
            return creditCard;
        }

        public static CreditCardModel New(string type, string number, int cv2, DateTime startDate, DateTime expiryDate, string issueNumber)
        {
            CreditCardModel card = new CreditCardModel(type, number, cv2, startDate, expiryDate, issueNumber);
            return card;
        }

        public static bool Save(CreditCardModel card)
        {
            //TODO:Add error handling
            try
            {
                using (OMSModelContainer context = new OMSModelContainer())
                {
                    CreditCard tmp = new CreditCard
                    {
                        Type = card._Type,
                        Number = card.Number,
                        Cv2 = card.Cv2,
                        StartDate = card.StartDate,
                        ExpiryDate = card.ExpiryDate,
                        IssueNumber = card.IssueNumber
                    };
                    context.CreditCards.AddObject(tmp);
                    context.SaveChanges();
                }
            }
            catch (DataException ex)
            {
                return false;
            }
            return true;
        }

        public static CreditCardModel Create(string type, string number, int cv2, DateTime startDate, DateTime expiryDate, string issueNumber)
        {
            CreditCardModel card = New(type, number, cv2, startDate, expiryDate, issueNumber);
            if (Save(card))
                return card;
            else
                return new CreditCardModel();  //TODO: Some value
        }

        public static CreditCardModel GetById(int id)
        {
            CreditCard card = GetCreditCardById(id);
            CreditCardModel cardModel = new CreditCardModel(card);
            return cardModel;
        }

        public static List<CreditCardModel> GetAll()
        {
            List<CreditCard> cards;
            using (OMSModelContainer context = new OMSModelContainer())
            {
                cards = context.CreditCards.ToList();
            }
            List<CreditCardModel> tmpCards = new List<CreditCardModel>();
            foreach (CreditCard i in cards)
            {
                tmpCards.Add(new CreditCardModel(i));
            }
            return tmpCards;
        }

        public static void Update(CreditCardModel cardModel)
        {
            try
            {
                using (OMSModelContainer context = new OMSModelContainer())
                {
                    CreditCard card = (from m in context.CreditCards where m.Id == cardModel.Id select m).First();
                    card.Type = cardModel._Type;
                    card.Number = cardModel.Number;
                    card.Cv2 = cardModel.Cv2;
                    card.StartDate = cardModel.StartDate;
                    card.ExpiryDate = cardModel.ExpiryDate;
                    card.IssueNumber = cardModel.IssueNumber;

                    context.CreditCards.ApplyCurrentValues(card);
                    context.SaveChanges();
                }
            }
            catch (DataException/**/)
            {
            }
        }

        public static void Delete(int id)
        {
            try
            {
                using (OMSModelContainer context = new OMSModelContainer())
                {
                    CreditCard card = (from m in context.CreditCards where m.Id == id select m).First();
                    context.CreditCards.DeleteObject(card);
                    context.SaveChanges();
                }
            }
            catch (DataException/**/)
            {
            }
        }

    }
}